package cn.m3a1.service.admin;

import cn.m3a1.bean.*;
import cn.m3a1.dao.StudentOfClassMapper;
import cn.m3a1.dao.TclassMapper;
import cn.m3a1.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wyj
 * @date 2019/7/3
 * @time 上午9:24
 */
@Service
public class TclassService {
    @Autowired
    TclassMapper tclassMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    StudentOfClassMapper studentOfClassMapper;
    public   List<Tclass>  getTclass(){

        TclassExample tclassExample=new TclassExample();
        TclassExample.Criteria criteria = tclassExample.createCriteria();

        List<Tclass> tclasses = tclassMapper.selectByExamplewithman(tclassExample);
        return tclasses;
    }
    public   Tclass  getTclass(int id){

        TclassExample tclassExample=new TclassExample();
        TclassExample.Criteria criteria = tclassExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Tclass> tclasses = tclassMapper.selectByExample(tclassExample);
        return tclasses.get(0);
    }
    public List<User> getMang(){

        UserExample userExample=new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andTypeEqualTo(70);
        List<User> users = userMapper.selectByExample(userExample);
            return users;
    }

    public  Boolean add(Tclass tclass){
        int insert = tclassMapper.insert(tclass);
        return  insert==1;
    }

    public  Boolean deltclass(int id){
        TclassExample tclassExample=new TclassExample();
        TclassExample.Criteria criteria = tclassExample.createCriteria();
        criteria.andIdEqualTo(id);
        int insert = tclassMapper.deleteByExample(tclassExample);
        return  insert==1;
    }

    public  Boolean uptclass(Tclass  tclass){
        TclassExample tclassExample=new TclassExample();
        TclassExample.Criteria criteria = tclassExample.createCriteria();
        criteria.andIdEqualTo(tclass.getId());
        int insert = tclassMapper.updateByExample(tclass,tclassExample);
        return  insert==1;
    }


    public List<User>  findstu(int id){
        List<User> users = userMapper.selectByExamplewithstu(id);
        return  users;
    }
    public String getClassName(int id){
        TclassExample tclassExample=new TclassExample();
        TclassExample.Criteria criteria = tclassExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Tclass> tclasses = tclassMapper.selectByExample(tclassExample);
        if (tclasses.isEmpty()){
            return "";
        }
        return  tclasses.get(0).getName();
    }

    public  Boolean deltStuClass(int id){
        StudentOfClassExample studentOfClassExample=new StudentOfClassExample();
        StudentOfClassExample.Criteria criteria = studentOfClassExample.createCriteria();
        criteria.andStudentidEqualTo(id);
        int i = studentOfClassMapper.deleteByExample(studentOfClassExample);
        return  i==1;
    }

}
